package org.crh.payment.impl;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.crh.common.ScheduleService;
import org.crh.payment.domain.PaymentOrder;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;

// 库存补偿消费者
@RocketMQMessageListener(topic = "STOCK_COMPENSATE_TOPIC", consumerGroup = "stock_compensate_group")
public class StockCompensateConsumer {
    @Resource
    private ScheduleService scheduleService;

    private RedissonClient redissonClient;

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private String redisPort;

    @Value("${spring.redis.password:}")
    private String password;

    public void onMessage(PaymentOrder order) {
        // 对比Redis和数据库库存
        String key =String.valueOf(order.getAppointmentId());
        RBucket<String> bucket = redissonClient.getBucket(key);
        Integer redisStock = Integer.valueOf(bucket.get());
        Integer dbStock = scheduleService.selectStock(order.getAppointmentId());
        if (redisStock < dbStock) {
            bucket.set(String.valueOf(dbStock));
        }
    }
}
